/********************************************************************
FileName    :   Dal_WorkGroup.cs
CreateTime  :   2015-10-27 04:10
BaseClass   :   Dal_WorkGroup
FileType    :   .cs
Author      :  minjie
Description :   Dal_WorkGroupDAL层（简单三层）
*********************************************************************/

using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Data;
namespace OS.DAL
{
	/// <summary>
	/// Dal_WorkGroup的摘要说明。
	/// </summary>
	public partial class Dal_WorkGroup 
  	{ 
     		      
  		public bool Exists(string WorkGroupId) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) from WorkGroup"); 
  			strSql.Append(" where "); 
           SqlParameter[] parameters = { new SqlParameter("@WorkGroupId", SqlDbType.Int,4)};  
  			return DbHelperSQL.Exists(strSql.ToString(),parameters); 
  		} 
  		 
  				 
  		 
  		/// <summary> 
  		/// 增加一条数据 
  		/// </summary> 
  		public  bool  Add(WorkGroup_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into WorkGroup(");			 
  	strSql.Append("WorkGroupId,PWorkGroupId,WorkGroupName,WorkGroupRemark,SortNum,WorkGroupIsShow,WorkGroupRestrict) values ("); 
  	strSql.Append("	@WorkGroupId,@PWorkGroupId,@WorkGroupName,@WorkGroupRemark,@SortNum,@WorkGroupIsShow,@WorkGroupRestrict	 ) "); 
  			SqlParameter[] parameters = { 
              new SqlParameter("@WorkGroupId", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@PWorkGroupId", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@WorkGroupName", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@WorkGroupRemark", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@SortNum", SqlDbType.Int,4) ,              
              new SqlParameter("@WorkGroupIsShow", SqlDbType.Int,4) ,              
              new SqlParameter("@WorkGroupRestrict", SqlDbType.VarChar,50)};             
           if (model.WorkGroupId != null)  
              parameters[0].Value = model.WorkGroupId;  
           else  
              parameters[0].Value = DBNull.Value;  
              
           if (model.PWorkGroupId != null)  
              parameters[1].Value = model.PWorkGroupId;  
           else  
              parameters[1].Value = DBNull.Value;  
              
           if (model.WorkGroupName != null)  
              parameters[2].Value = model.WorkGroupName;  
           else  
              parameters[2].Value = DBNull.Value;  
              
           if (model.WorkGroupRemark != null)  
              parameters[3].Value = model.WorkGroupRemark;  
           else  
              parameters[3].Value = DBNull.Value;  
              
           if (model.SortNum != null)  
              parameters[4].Value = model.SortNum;  
           else  
              parameters[4].Value = DBNull.Value;  
              
           if (model.WorkGroupIsShow != null)  
              parameters[5].Value = model.WorkGroupIsShow;  
           else  
              parameters[5].Value = DBNull.Value;  
              
           if (model.WorkGroupRestrict != null)  
              parameters[6].Value = model.WorkGroupRestrict;  
           else  
              parameters[6].Value = DBNull.Value;  
              
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);			 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
              return false;; 
           }  
  		} 
  		 
  		/// <summary> 
  		/// 增加一条数据(字符串) 
  		/// </summary> 
  		public  string  AddStr(WorkGroup_Entity model) 
  		{ 
  				if(model.WorkGroupId==null) 
  				{ 
  					model.WorkGroupId="";  
  				} 
  				if(model.PWorkGroupId==null) 
  				{ 
  					model.PWorkGroupId="";  
  				} 
  				if(model.WorkGroupName==null) 
  				{ 
  					model.WorkGroupName="";  
  				} 
  				if(model.WorkGroupRemark==null) 
  				{ 
  					model.WorkGroupRemark="";  
  				} 
  				if(model.SortNum==null) 
  				{ 
  					model.SortNum=0; 
  				} 
  				if(model.WorkGroupIsShow==null) 
  				{ 
  					model.WorkGroupIsShow=0; 
  				} 
  				if(model.WorkGroupRestrict==null) 
  				{ 
  					model.WorkGroupRestrict="";  
  				} 
  		 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into WorkGroup(");			 
  	strSql.Append("WorkGroupId,PWorkGroupId,WorkGroupName,WorkGroupRemark,SortNum,WorkGroupIsShow,WorkGroupRestrict) values ("); 
  	strSql.Append("	'"+ model.WorkGroupId+"','"+ model.PWorkGroupId+"','"+ model.WorkGroupName+"','"+ model.WorkGroupRemark+"',"+ model.SortNum+","+ model.WorkGroupIsShow+",'" +model.WorkGroupRestrict+"'	 ) "); 
           return strSql.ToString(); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 更新一条数据 
  		/// </summary> 
  		public bool Update(WorkGroup_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("update WorkGroup set "); 
              strSql.Append(" WorkGroupId = @WorkGroupId,"); 
              strSql.Append(" PWorkGroupId = @PWorkGroupId,"); 
              strSql.Append(" WorkGroupName = @WorkGroupName,"); 
              strSql.Append(" WorkGroupRemark = @WorkGroupRemark,"); 
              strSql.Append(" SortNum = @SortNum,"); 
              strSql.Append(" WorkGroupIsShow = @WorkGroupIsShow,"); 
              strSql.Append(" WorkGroupRestrict = @WorkGroupRestrict"); 
  			strSql.Append(" where  WorkGroupId=@WorkGroupId "); 
  						 
  SqlParameter[] parameters = { 
              new SqlParameter("@WorkGroupId", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@PWorkGroupId", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@WorkGroupName", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@WorkGroupRemark", SqlDbType.NVarChar,255) ,              
              new SqlParameter("@SortNum", SqlDbType.Int,4) ,              
              new SqlParameter("@WorkGroupIsShow", SqlDbType.Int,4) ,              
              new SqlParameter("@WorkGroupRestrict", SqlDbType.VarChar,50)};             
           if (model.WorkGroupId != null)  
              parameters[0].Value = model.WorkGroupId;  
           else  
              parameters[0].Value = DBNull.Value;  
              
           if (model.PWorkGroupId != null)  
              parameters[1].Value = model.PWorkGroupId;  
           else  
              parameters[1].Value = DBNull.Value;  
              
           if (model.WorkGroupName != null)  
              parameters[2].Value = model.WorkGroupName;  
           else  
              parameters[2].Value = DBNull.Value;  
              
           if (model.WorkGroupRemark != null)  
              parameters[3].Value = model.WorkGroupRemark;  
           else  
              parameters[3].Value = DBNull.Value;  
              
           if (model.SortNum != null)  
              parameters[4].Value = model.SortNum;  
           else  
              parameters[4].Value = DBNull.Value;  
              
           if (model.WorkGroupIsShow != null)  
              parameters[5].Value = model.WorkGroupIsShow;  
           else  
              parameters[5].Value = DBNull.Value;  
              
           if (model.WorkGroupRestrict != null)  
              parameters[6].Value = model.WorkGroupRestrict;  
           else  
              parameters[6].Value = DBNull.Value;  
              
              int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 删除一条数据 
  		/// </summary> 
  		public bool Delete(string  WorkGroupId) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from WorkGroup "); 
  			strSql.Append(" where  WorkGroupId=@WorkGroupId"); 
  			SqlParameter[] parameters = {  new SqlParameter("@WorkGroupId", SqlDbType.NVarChar,255) };  
    parameters[0].Value = WorkGroupId;  
   
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 批量删除一批数据 
  		/// </summary> 
  		public bool DeleteList(string WorkGroupIdlist ) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from WorkGroup "); 
  			strSql.Append(" where ID in ("+WorkGroupIdlist + ")  "); 
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString()); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 得到一个对象实体 
  		/// </summary> 
  		public WorkGroup_Entity GetModel(string   WorkGroupId) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select  WorkGroupId,PWorkGroupId,WorkGroupName,WorkGroupRemark,SortNum,WorkGroupIsShow,WorkGroupRestrict");	
		strSql.Append("  from WorkGroup"); 
  			strSql.Append(" where  WorkGroupId=@WorkGroupId "); 
  			SqlParameter[] parameters = {  new SqlParameter("@WorkGroupId", SqlDbType.NVarChar,255) };  
    parameters[0].Value = WorkGroupId;  
  			 
  			WorkGroup_Entity model=new WorkGroup_Entity(); 
  			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters); 
  			 
  			if(ds!=null&&ds.Tables[0].Rows.Count>0) 
  			{ 
  		DataTable dt=ds.Tables[0];	  
  				model.WorkGroupId= dt.Rows[0]["WorkGroupId"].ToString(); 
  				model.PWorkGroupId= dt.Rows[0]["PWorkGroupId"].ToString(); 
  				model.WorkGroupName= dt.Rows[0]["WorkGroupName"].ToString(); 
  				model.WorkGroupRemark= dt.Rows[0]["WorkGroupRemark"].ToString(); 
  				if(dt.Rows[0]["SortNum"]!=null&&dt.Rows[0]["SortNum"].ToString()!="") 
  				{ 
  					model.SortNum=int.Parse(dt.Rows[0]["SortNum"].ToString()); 
  				} 
  				if(dt.Rows[0]["WorkGroupIsShow"]!=null&&dt.Rows[0]["WorkGroupIsShow"].ToString()!="") 
  				{ 
  					model.WorkGroupIsShow=int.Parse(dt.Rows[0]["WorkGroupIsShow"].ToString()); 
  				} 
  				model.WorkGroupRestrict= dt.Rows[0]["WorkGroupRestrict"].ToString(); 
  				return model; 
  			} 
  			else 
  			{ 
  				return null; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 分页获取数据列表 
  		/// </summary> 
  		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  		 strSql.Append("SELECT * FROM ( "); 
  		 strSql.Append(" SELECT ROW_NUMBER() OVER ("); 
  if (!string.IsNullOrEmpty(orderby.Trim())) 
 { 
    strSql.Append("order by T." + orderby); 
 } 
else  
 { 
     strSql.Append("order by T.WorkGroupId desc"); 
 } 
		 strSql.Append(")AS Row, T.*  from WorkGroup T "); 
  		 if (!string.IsNullOrEmpty(strWhere.Trim()))  
  			{ 
  			 strSql.Append(" WHERE " + strWhere); 
  			} 
  		    strSql.Append(" ) TT"); 
  	        strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 获得数据列表 
  		/// </summary> 
  		public DataSet GetList(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select * "); 
  			strSql.Append(" FROM WorkGroup "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得前几行数据 
  		/// </summary> 
  		public DataSet GetList(int Top,string strWhere,string filedOrder) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select "); 
  			if(Top>0) 
  			{ 
  				strSql.Append(" top "+Top.ToString()); 
  			} 
  			strSql.Append(" * "); 
  			strSql.Append(" FROM WorkGroup "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			strSql.Append(" order by " + filedOrder); 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得记录条数 
  		/// </summary> 
  		public int GetRecordCount(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) "); 
  			strSql.Append(" FROM WorkGroup "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  		   object obj=	DbHelperSQL.GetSingle(strSql.ToString()); 
  			if(obj!=null && obj.ToString()!="") 
  			{ 
              	return Convert.ToInt32(obj); 
  			} 
  			else 
  			return 0; 
  		} 
   
      
  	} 
  
}
